package com.baizhi.aop;


import com.baizhi.annotation.LogAnnotation;
import com.baizhi.dao.LogDao;
import com.baizhi.entity.Admin;
import com.baizhi.entity.Log;
import com.baizhi.service.LogService;
import com.baizhi.utils.IPKit;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.logging.Logger;

/**
 * Created by 野马 on 2019/6/19.
 * Configuration  标记当前类有配置类
 * Aspect  标记当前类为切面类   会自动编织类中的增强和切点
 */
@Configuration
@Aspect
public class LogAspect {

    @Autowired
    private LogService logService;

    private org.slf4j.Logger logger = LoggerFactory.getLogger(LogAspect.class);


    /**
     * 定义切入点
     * 定义为注解
     * @annotation(com.baizhi.annotation.LogAnnotation)切注解的切入点表达式
     *
     */
    @Pointcut(value = "@annotation(com.baizhi.annotation.LogAnnotation)")
    public void logPoint(){}

    /**
     * 定义增强
     *
     */
    @After("logPoint()")
    public void logAfter(JoinPoint joinPoint) throws IOException {
        logger.info("aop开始");
        //1 获取创建一个Log对象的所有数据
        Log log = new Log();

        //1,1 获取时间
        log.setLogDate(new Date());

        //1.2  获取管理员的名字 session
        // 拿当前线程对应的request HttpServletRequest
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        HttpSession session = request.getSession();
        Admin admin = (Admin) session.getAttribute("admin");
        log.setUsername(admin.getUsername());

        //1.3  获取ip地址
        String ipAddressrequest = IPKit.getIpAddress(request);
        log.setLogIp(ipAddressrequest);

        //1.4 获取操作记录
        /**
         * 不同方法的操作记录不一样
         *
         * 使用自定义注解
         * 1.切点
         * 2.注解中可以写值  操作记录  在增强内部获取注解的值
         *
         * 获取注解中的值
         *
         */
        //1.获取方法的签名对象
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        //2.得到方法对象
        Method method = signature.getMethod();
        //3.得到方法上的注解
        LogAnnotation annotation = method.getAnnotation(LogAnnotation.class);
        //4得到注解中的值
        String value = annotation.value();
        log.setLogContent(value);

        logService.insertLog(log);

        logger.info("aop执行结束");
    }

}
